package edu.zut.cs.software.person.customer.service;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.person.customer.domain.Customer;
import edu.zut.cs.software.teach.Base.service.GenericGenerator;

/**
 * 
 * @author wenmanke
 *
 */

public class CustomerEntityGenerator extends GenericGenerator{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(CustomerEntityGenerator.class.getName());

	/**
	 * Logger for this class
	 */
	List<Customer> customerList;
	
	CustomerManager customermanager;
	

	@Autowired
	public void setCustomermanager(CustomerManager customermanager) {
		this.customermanager = customermanager;
	}

	@Before
	public void setUp() throws Exception {
		this.customerList = new ArrayList<Customer>();
		InputStream input = CustomerManagerTest.class.getResourceAsStream("/sms_customer_software.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		for (int  i=1;i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Customer s = new Customer();
			for (int j= 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						s.setName(value);
				}
			}
			this.customerList.add(s);
		}
	}
	
	@Test
	public void test() {
		this.customermanager.deleteAll();
		this.customermanager.save(this.customerList);
		Iterable<Customer> result = this.customermanager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - Iterable<Customer> result={}", result); //$NON-NLS-1$
		}
		
	}

}

